﻿<!DOCTYPE html>
<html>
<head>
    <script src="../../js/header.js"></script>
    <script src="../../lib/multiselect/jquery.multi-select.js"></script>
    <link rel="stylesheet" type="text/css" href="../../lib/multiselect/jquery.multi-select.css" />
</head>
<body>
    <section class="container-fluid" style="background-color: white;">
        <form class="form-horizontal m15_t" id="form">
            <div class="tab-content">
                <div class="tab-pane fade in active" id="divCompany">
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>公司名称</label>
                        <select class="m15_l" name="uCompanyID" id="uCompanyID">
                            <option value="">请选择</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">角色名称</label>
                        <div class="wd250 m15_l">
                            <select id='sltRoleNames' multiple='multiple' name="uRoleName" class="m15_l"></select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>登陆名称：</label>
                        <div class="wd250">
                            <input class="form-control" type="text" name="uLoginName"
                                   readonly onfocus="this.removeAttribute('readonly');" maxlength="20" placeholder="长度必须介于 6 和 20 之间" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr" id="showPwdAttr">*</span> 密 码：</label>
                        <div class="input-group wd250">
                            <input class="form-control" type="password" name="uPwd" readonly onfocus="this.removeAttribute('readonly');" maxlength="20" placeholder="长度必须介于 6 和 20 之间" />
                            <span class="input-group-addon" style="margin-left:20px;" onclick="changeInputType(this);">
                                <i class="glyphicon glyphicon-eye-open"></i>
                            </span>
                        </div>
                    </div>

                    <div class="form-group" id="divNewPwd">
                        <label class="wd100 control-label"><span class="WdateFmtErr" id="showNewPwdAttr">*</span>新密码：</label>
                        <div class="input-group wd250">
                            <input class="form-control" type="password" name="uNewPwd" maxlength="20" placeholder="长度必须介于 6 和 20 之间" />
                            <span class="input-group-addon" style="margin-left:20px;" onclick="changeInputType(this);">
                                <i class="glyphicon glyphicon-eye-open"></i>
                            </span>
                        </div>
                    </div>
                    <div class="form-group" id="divAgainPwd">
                        <label class="wd100 control-label"><span class="WdateFmtErr" id="showAgainPwdAttr">*</span>确认密码：</label>
                        <div class="input-group wd250">
                            <input class="form-control" type="password" name="uAgainPwd" onfocus="this.removeAttribute('readonly');" maxlength="20" placeholder="必须与新密码保持一致" />
                            <span class="input-group-addon" style="margin-left:20px;" onclick="changeInputType(this);">
                                <i class="glyphicon glyphicon-eye-open"></i>
                            </span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label">联络电话：</label>
                        <div class="wd250">
                            <input class="form-control" type="text" name="uTelephone" maxlength="40" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">联络Email：</label>
                        <div class="wd250">
                            <input class="form-control" type="text" name="uEmail" maxlength="50" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">说明：</label>
                        <div class="wd250">
                            <textarea style="height: auto;font-size: 12px;width: 100%;" type="text" id="uDescription" name="uDescription" maxlength="200">
                             </textarea>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>
    <script type="text/javascript">
        var validator = null;
        $(function (e) {
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                showUserInfo(id);
                $("#showPwdAttr").hide(); $("#showNewPwdAttr").hide(); $("#showAgainPwdAttr").hide();

                $('input[name="uPwd"]').blur(function () {
                    var thisVal = $(this).val();
                    if (thisVal != "" & thisVal != undefined) {
                        $('input[name="uNewPwd"]').removeAttr("disabled");
                        $('input[name="uAgainPwd"]').removeAttr("disabled");
                    }
                })
            } else {
                $("#divNewPwd").remove();
                showCompanySelect({ companyStatus: 1 });
                showRoleSelect({});
            }
            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    uCompanyID: { required: true },
                    uLoginName: {
                        required: true,
                        rangelength: [6, 20],
                        specialChar: true
                    },
                    uPwd: {
                        rangelength: [6, 20]
                    },
                    uNewPwd: {
                        rangelength: [6, 20]
                    },
                    uAgainPwd: {
                        rangelength: [6, 20]
                    },
                    uTelephone: {
                        isPhone: true
                    },
                    uEmail: {
                        isemail: true
                    }
                    , uDescription: {
                        specialChar: true
                    }
                },
                messages: {
                    uCompanyID: { required: "请选择一家公司" },
                    uLoginName: {
                        required: "登陆名称不能为空",
                        rangelength: "登陆名称长度必须介于 6 和 20 之间",
                        specialChar: "登陆名称不能带有特殊字符"
                    },
                    uPwd: {
                        //required: "密码不能为空",
                        rangelength: "密码长度必须介于 6 和 20 之间",
                    },
                    uNewPwd: {
                        rangelength: "新密码长度必须介于 6 和 20 之间",
                    },
                    uAgainPwd: {
                        rangelength: "重复密码长度必须介于 6 和 20 之间"
                    },

                    uTelephone: {
                        isPhone: "请正确填写您的联络电话",
                    },
                    uEmail: {
                        isemail: "Email格式不正确"
                    },
                    uDescription: {
                        specialChar:"说明内容不能带有特殊字符"
                    }
                }
            });

            $("#uCompanyID").change(function () {
                var id = $(this).val();
                if (id != "") {
                    showRoleSelect({ CompanyId: id });
                }
            })
        });

        //显示公司下拉列表
        function showCompanySelect(dataModel) {
            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Company/Get",
                data: JSON.stringify(dataModel),
                dataType: "json",
                async: false,
                contentType: "application/json",
                success: function (data) {
                    if (data.totalCount > 0) {
                        var infos = data.result;
                        var html = "";
                        for (var i = 0; i < infos.length; i++) {
                            html += "<option value=" + infos[i].companyID + ">" + infos[i].companyName + "</option>";
                        }
                        $("#uCompanyID").html('<option value="">请选择</option>').append(html);
                    }
                }
            });
        }

        //显示角色下拉列表
        function showRoleSelect(dataModel, list = null) {
            FlashPay.Util.Ajax({
                type: "get",
                url: "/Role/GetRoleList",
                data: dataModel,
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.success) {
                        var infos = data.data;
                        var html = "";
                        for (var i = 0; i < infos.length; i++) {
                            html += "<option value=" + infos[i].rId + ">" + infos[i].rName + "</option>";
                        }
                        $("#sltRoleNames").html('').append(html);
                    } else {
                        $("#sltRoleNames").html('').html('<option value="" disabled>请选择</option>');
                    }

                    if (list != null) {
                        var newArr = new Array();
                        $.each(list, function (e, v) {
                            newArr.push(v.urRid);

                        })
                        $('#sltRoleNames').val(newArr);
                    }
                    $('#sltRoleNames').multiSelect("destroy").multiSelect({ selectionHeader: '已选择' });
                }
            });
        }

        //显示用户详细信息
        function showUserInfo(id) {
            FlashPay.Util.Ajax({
                type: "get",
                url: "/User/GetList",
                data: { Id: id, uStatus: -1 },
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.totalCount > 0) {
                        var infos = data.tData[0];
                        FlashPay.Util.SetInputVal("uLoginName", infos.uLoginName);
                        $("input[name='uLoginName']").attr("disabled", true);

                        FlashPay.Util.SetInputVal("uTelephone", infos.uTelephone);
                        FlashPay.Util.SetInputVal("uEmail", infos.uEmail);
                        $("#uDescription").val(infos.uDescription);

                        var companyID = infos.uCompanyId;
                        showCompanySelect({});
                        $("#uCompanyID").find("option[value=" + companyID + "]").attr("selected", true);

                        //显示已选中的角色值
                        var roleList = infos.userRole;
                        //显示角色下拉列表
                        showRoleSelect({ companyId: companyID }, roleList);
                    }
                }
            });
        }

        function save() {
            if (!validator.form()) {
                return false;
            }
            var userViewModel = {};
            userViewModel.uTelephone = $('input[name="uTelephone"]').val();
            userViewModel.uEmail = $("input[name='uEmail']").val();
            userViewModel.uDescription = $.trim($("#uDescription").val());
            userViewModel.CompanyID = $("#uCompanyID").val();

            var uPwd = $('input[name="uPwd"]').val();
            var uNewPwd = $('input[name="uNewPwd"]').val();
            var uAgainPwd = $('input[name="uAgainPwd"]').val();
            var roles = $("#sltRoleNames").val();

            var url = "/User/SaveUserInfo";
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                userViewModel.Id = id;
                if (uPwd != "") {
                    if (uNewPwd == "") {
                        FlashPay.UI.Tip_short_warning("请输入新密码!");
                        $('input[name="uNewPwd"]').focus();
                        return;
                    }
                    if (uAgainPwd == "") {
                        FlashPay.UI.Tip_short_warning("请输入确认密码!");
                        $('input[name="uAgainPwd"]').focus();
                        return;
                    }
                    if (uAgainPwd != uNewPwd) {
                        FlashPay.UI.Tip_short_warning("确认密码与新密码不一致!");
                        return;
                    }
                } else {
                    if (uNewPwd != "" || uAgainPwd != "") {
                        FlashPay.UI.Tip_short_warning("请输入密码!");
                        $('input[name="uPwd"]').focus();
                        return;
                    }
                }
            } else {
                userViewModel.uLoginName = $("input[name='uLoginName']").val();
                if (uPwd == "") {
                    FlashPay.UI.Tip_short_warning("请输入密码!");
                    $('input[name="uPwd"]').focus();
                    return;
                }
                if (uAgainPwd == "") {
                    FlashPay.UI.Tip_short_warning("请输入确认密码!");
                    $('input[name="uAgainPwd"]').focus();
                    return;
                }
                if (uAgainPwd != uPwd) {
                    FlashPay.UI.Tip_short_warning("确认密码输入错误!");
                    $('input[name="uAgainPwd"]').focus();
                    return;
                }
            }

            userViewModel.uPwd = uPwd;
            userViewModel.uAgainPwd = uAgainPwd;
            userViewModel.userRole = roles;
            AddOrEdit(url, userViewModel);
        }

        function AddOrEdit(url, userViewModel) {
            FlashPay.Util.Ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(userViewModel),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (!data.success) {
                        FlashPay.UI.Tip_short_warning(data.errorMessage == null ? "操作失败！" : data.errorMessage);
                        return;
                    } else {
                        FlashPay.UI.DialogOpener().Tip_success(data.successMessage == null ? "操作成功！" : data.successMessage);
                        FlashPay.UI.DialogOpener().doSearch();
                        FlashPay.UI.CloseDialog();
                    }
                }
            });
        }
        function changeInputType(now) {
            var prevNow = $(now).prev();
            if (prevNow.attr('type') == 'text') {
                prevNow.attr('type', 'password');
            } else {
                prevNow.attr('type', 'text');
            }
        }
    </script>
</body>
</html>